<?php
/**
 * Album record
 *
 * @author Lukas Normantas
 */
class Content_Model_Album extends Doctrine_Record{
    public function setTableDefinition()
    {
        $this->setTableName('content_albums');
        $this->hasColumn('title', 'string', 50);
    }

    public function setUp()
    {
        $this->actAs('Timestampable');

        $this->hasMany('Content_Model_Picture as Pictures', array(
            'local' => 'id',
            'foreign' => 'album_id'
        ));

    }
    public function getAll()
    {
        $q = Doctrine_Query::create()
               ->from( get_class($this) . ' a' );
        $result = $q->execute(array(), Doctrine::HYDRATE_RECORD);
        return $result;
    }

    public function getPictures()
    {
        $q = Doctrine_Query::create()
               ->from( 'Content_Model_Picture p' )
               ->where('album_id = ?', $this->id)
               ->orderBy('position');
        $result = $q->execute(array(), Doctrine::HYDRATE_RECORD);
        return $result;
    }

    protected $_editingForm = null;

    public function getEditingForm(){
        if ($this->_editingForm == null){
            $this->_editingForm = new Content_Form_AlbumEditing();
            $this->_editingForm->setDefaults($this->toArray());
        }
        return $this->_editingForm;
    }

    public function edit($data)
    {
        $form = $this->getEditingForm();
        if ($form->isValid( $data )){
            $this->fromArray($form->getValues());
            $this->save();       
            return true;
        }
        return false;
    }


    static public function getAlbumSelectData(){
        $album_collection = Doctrine_Query::create()
            ->from('Content_Model_Album a')
            ->execute(null, Doctrine::HYDRATE_RECORD);
        $result = array(0=>'');
        foreach ($album_collection as $album){
            $result[$album['id']] = $album['title'];
        }
        return $result;

    }



}

